﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MSOutlook = Microsoft.Office.Interop.Outlook;
using Utility = Microsoft.Sample.Outlook.Utility;
using System.Diagnostics;

namespace Microsoft.Sample.Outlook.CRMIntegration.FolderHomePages
{
    [System.Runtime.InteropServices.ComVisible(true)]
    [System.Runtime.InteropServices.Guid("5611FC70-D5B7-446c-A225-48F3EA957F1F")]
    public partial class PersonalFolderToday : UserControl
    {
#if !INSTALL
        //These registration methods are only called when compiling w/ VS
        //For installer scenarios, the RegisterSafeforScripting call is 
        //executed by the installer class
        [System.Runtime.InteropServices.ComRegisterFunction()]
        public static void RegisterServer(Type t)
        {
            Utility.FolderHomePage.RegisterSafeForScripting(t);
        }

        [System.Runtime.InteropServices.ComUnregisterFunction()]
        public static void UnregisterServer(Type t)
        {
            Utility.FolderHomePage.UnregisterSafeForScripting(t);
        }
#endif
        
        public PersonalFolderToday()
        {
            InitializeComponent();
            ThisApplication.ConfigureDataDirectory(this.GetType());
        }

        MSOutlook.Application _app;

        public void Initialize(MSOutlook.Application o, string _strAccountID)
        {
            Guid _accountID = new Guid(_strAccountID);
            Utility.GlobalClass.AccountID = _accountID;
            Utility.GlobalClass.AccountName = (string)this.accountsTableAdapter.GetAccountName(_accountID);
            _app = (MSOutlook.Application)o;

            MSOutlook.Explorer activeExplorer = _app.ActiveExplorer();
            string rootFolder = activeExplorer.CurrentFolder.FolderPath + "\\";

            InitOutlookView(rootFolder + Utility.GlobalClass.AccountName+ "'s" + Properties.Resources.ContactsFolderName, tabPageContacts);
            InitOutlookView(rootFolder + Utility.GlobalClass.AccountName + "'s" + Properties.Resources.AppointmentsFolderName, tabPageAppointments);

            try
            {
                //must update outlookID to row 
                string temp1 = (string)accountsTableAdapter.GetOutlookID(Utility.GlobalClass.AccountID);
                string temp2 = activeExplorer.CurrentFolder.EntryID;
                Guid temp3 = Utility.GlobalClass.AccountID;
                if (temp1 != temp2)
                {
                    accountsTableAdapter.UpdateOutlookID(activeExplorer.CurrentFolder.EntryID, Utility.GlobalClass.AccountID);
                }
                accountsTableAdapter.FillByOutlookID(pIMDataSet.Accounts, activeExplorer.CurrentFolder.EntryID);
            }
            catch (Exception ex)
            {
                MessageBox.Show("The PersonalFolderToday page couldn't access the database", "Outlook PIM Integration");
                ThisApplication.LogException(ex);

            }
        }

        private static void InitOutlookView(string folderName, TabPage tab)
        {
            Utility.OutlookViewControl view = new Utility.OutlookViewControl();
            view.Dock = DockStyle.Fill;
            view.Folder = folderName;
            tab.Controls.Add(view);
        }

        private void accountsBindingSource_CurrentItemChanged(object sender, EventArgs e)
        {
            labelMoreAddressValue.Text = string.Empty;
            labelNameValue.Text = string.Empty;

            DataRowView view = (DataRowView)accountsBindingSource.Current;
            PIMDataSet.AccountsRow row = (PIMDataSet.AccountsRow)view.Row;

            string city = string.Empty;
            if (!row.IsCityNull())
                city = row.City + ", ";

            string stateRegion = string.Empty;
            if (!row.IsStateRegionNull())
                stateRegion = row.StateRegion + " ";

            string zip = string.Empty;
            if (!row.IsPostalCodeNull())
                zip = row.PostalCode;

            string firstName = string.Empty;
            if (!row.IsFirstNameNull())
                firstName = row.FirstName;

            string lastName = string.Empty;
            lastName = row.LastName;

            labelMoreAddressValue.Text = city + stateRegion + zip;
            labelNameValue.Text = firstName + "  " + lastName;
        }

        private void linkLabelWebPageValue_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            ProcessStartInfo sInfo = new ProcessStartInfo(((LinkLabel)sender).Text);
            Process.Start(sInfo);
        }
    }
}
